{extend name="public:base" /}

{block name="body"}
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-header layuiadmin-card-header-auto">
        <button class="layui-btn layuiadmin-btn-database" data-type="back">备份</button>
      </div>
	  	
	  <div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="demo">
		  <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
		</div>
	 
      <div class="layui-card-body">    
        <table id="LAY-app-content-database" lay-filter="LAY-app-content-database"></table>
		<script type="text/html" id="toolbarDemo">
		  <div class="layui-btn-container">
			<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
			<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
			<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
		  </div>
		</script>
        <script type="text/html" id="tao-database">
		  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="down"><i class="layui-icon layui-icon-edit"></i>下载</a>
		  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-edit"></i>删除</a>
        </script>
      </div>
    </div>
  </div>

{/block}

{block name="js"}
  <script>
 
  layui.config({
    base: '/static/admin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table','layer','element'], function(){

    var table = layui.table,
	layer = layui.layer,
	element = layui.element;
    var $ = layui.$;

	var active = {
	
		back: function backup(predate)
		{
			var index = layer.load();
			$.ajax({
				url: "{:url('admin/Database/backup')}",
				type: 'POST', //POST
				async: true, //或false,是否异步
				dataType: 'json',
				data:predate,
				success: function (data, textStatus, jqXHR) {
					
					if (data.totalpercentage >= 100)
					{
						//关闭
						layer.close(index);  
						layer.alert('备份完成', {icon: 1});
						table.reload('LAY-app-content-database'); //数据刷新
					}
						
					element.progress('demo', data.totalpercentage+'%');

					//循环备份
					if (data.totalpercentage < 100)
					{
						backup(data);
					}

				}	
					
			});		
		}

	};
	
	
	
	//总帐信息
    table.render({
        elem: '#LAY-app-content-database'
        ,url: "{:url('admin/Database/index')}"	//进列表接口
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,cols: [[
            {type: 'checkbox', fixed: 'left'}
			,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
            ,{field: 'time', title: '备份时间',width: 180}
			,{field: 'name', title: '备份名', minWidth: 200}
            ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#tao-database'}
        ]]
		,page: true
		,limit: 15
		,limits: [10, 15, 20, 25, 30]
        ,text: '对不起，加载出现异常！'
    });
	
	//监听工具条
    table.on('tool(LAY-app-content-database)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('确定删除此站点？', function(index){
                $.ajax({
                    type:'post',
                    url:"{:url('Database/delete')}",
                    data:{name:data.name},
                    dataType:'json',
                    success:function(data){
                        if(data.code == 0){
                            layer.msg(data.msg,{
                                icon:6,
                                time:2000
                            },function(){
                                location.reload();
                            });
                        } else {
                            layer.open({
                                title:'删除失败',
                                content:data.msg,
                                icon:5,
                                adim:6
                            })
                        }
                    }
                });
                //obj.del();
                layer.close(index);
            });
        } else if(obj.event === 'down'){
            var tr = $(obj.tr);
            layer.open({
                type: 2
                ,title: '编辑支出'
                ,content: "{:url('Database/down')}" + '?id='+ data.id
                ,area: ['420px', '460px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
				
                    //获取iframe元素的值
					var iframeWindow = window['layui-layer-iframe'+ index]
				  ,submit = layero.find('iframe').contents().find("#LAY-admin-pay-submit");

				  //监听提交
				  iframeWindow.layui.form.on('submit(LAY-admin-pay-submit)', function(data){
					var field = data.field; //获取提交的字段
					
					//提交 Ajax 成功后，静态更新表格中的数据
					$.ajax({
						type:"post",
						url:payEdit,
						data:field,
						daType:"json",
						success:function (res){
							if (res.code == 0) {
								layer.msg(res.msg,{
									icon:6,
									time:2000
								});
							} else {
								layer.open({
									title:'修改失败',
									content:res.msg,
									icon:5,
									anim:6
								});
							}
						}
					});
					
					table.reload('LAY-app-content-pay'); //数据刷新
					layer.close(index); //关闭弹层
				  });  
				  
				  submit.trigger('click');
					
                }
                ,success: function(layero, index){
                    //给iframe元素赋值
                    var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-pay").click();
                    othis.find('input[name="pay_value"]').val(Math.abs(data.pay_value))
                    ,othis.find('input[name="pay_name"]').val(data.pay_name);
                }
            });
        }
    });
	
	
	
	
	$('#back1').on('click',function(){
	
		//询问框
		layer.confirm('确定备份？', {
		  btn: ['确定','取消'] //按钮
		  }, function(){
		  console.log(123);
			backup({});
			//layer.msg('的确很重要', {icon: 1});
			
		}
		);
	
	});
	

	
    $('.layui-btn.layuiadmin-btn-database').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>
{/block}
